package com.timber.util;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

import com.borland.jbcl.layout.*;


public class DBWaitDialog extends JDialog
{
  private static final String DEFAULT_MESSAGE =
    "Retrieving data. Please wait...";

  protected String message = DEFAULT_MESSAGE;
  protected JPanel mainPanel = new JPanel();
  protected BorderLayout borderLayout1 = new BorderLayout();
  protected JPanel topPanel = new JPanel();
  protected FlowLayout flowLayout1 = new FlowLayout();
  protected JLabel topLabel = new JLabel();
  protected JPanel notePanel = new JPanel();
  protected JLabel noteLabel = new JLabel();
  protected JLabel gifLabel = new JLabel();
  protected JPanel bottomPanel = new JPanel();
  protected VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  protected JPanel buttonPanel = new JPanel();
  protected JSeparator jSeparator1 = new JSeparator();
  protected FlowLayout flowLayout3 = new FlowLayout();
  protected JButton cancelBtn = new JButton();
  protected VerticalFlowLayout verticalFlowLayout2 = new VerticalFlowLayout();
  protected Window parent;
  protected Object retObj;
  protected Cancelable taskToCancel;
  protected SwingWorker dbWorker;
  protected ImageIcon waitImage;


  public DBWaitDialog(JFrame parent, Cancelable taskToCancel)
  {
    this(parent, taskToCancel, null);
  }

  public DBWaitDialog(JFrame parent, Cancelable taskToCancel, SwingWorker dbWorker)
  {
    super( parent, true );

    this.parent = parent;
    this.taskToCancel = taskToCancel;
    this.dbWorker = dbWorker;

    initialize();
  }

  public DBWaitDialog(JDialog parent, Cancelable taskToCancel)
  {
    this(parent, taskToCancel, null);
  }

  public DBWaitDialog(JDialog parent, Cancelable taskToCancel, SwingWorker dbWorker)
  {
    super( parent, true );

    this.parent = parent;
    this.taskToCancel = taskToCancel;
    this.dbWorker = dbWorker;

    initialize();
  }

  public DBWaitDialog(JFrame parent, Cancelable taskToCancel, SwingWorker dbWorker, String message)
  {
    this(parent, taskToCancel, dbWorker);
    setMessage(message);
  }

  private void initialize()
  {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try
    {
      jbInit();
      pack();
    }
    catch( Exception ex )
    {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception
  {
    this.setTitle("Please wait");
    this.setModal(true);

    waitImage = ImageMgr.getIcon(ImageMgr.WAIT_ICON);

    mainPanel.setLayout(borderLayout1);
    topPanel.setLayout(flowLayout1);
    topLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 16));
    topLabel.setForeground(Color.blue);
    topLabel.setText(message);
    notePanel.setLayout(verticalFlowLayout2);
    bottomPanel.setLayout(verticalFlowLayout1);
    buttonPanel.setLayout(flowLayout3);
    cancelBtn.setText("Cancel");
    if (taskToCancel != null) {
      cancelBtn.addActionListener(new java.awt.event.ActionListener() {
	  public void actionPerformed(ActionEvent e) {
	    cancelBtn_actionPerformed(e);
	  }
	});
    } else {
      cancelBtn.setEnabled(false);
    }
    noteLabel.setHorizontalAlignment(SwingConstants.CENTER);
    noteLabel.setHorizontalTextPosition(SwingConstants.CENTER);
    noteLabel.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
    noteLabel.setForeground(Color.black);
    noteLabel.setText("                    ");
    verticalFlowLayout2.setAlignment(VerticalFlowLayout.MIDDLE);
    gifLabel = new JLabel(waitImage);
    gifLabel.setHorizontalAlignment(SwingConstants.CENTER);
    gifLabel.setHorizontalTextPosition(SwingConstants.CENTER);
    getContentPane().add(mainPanel);
    mainPanel.add(topPanel, BorderLayout.NORTH);
    topPanel.add(topLabel, null);
    mainPanel.add(notePanel, BorderLayout.CENTER);
    notePanel.add(noteLabel, null);
    notePanel.add(gifLabel, null);
    mainPanel.add(bottomPanel, BorderLayout.SOUTH);
    bottomPanel.add(jSeparator1, null);
    bottomPanel.add(buttonPanel, null);
    buttonPanel.add(cancelBtn, null);
  }

  public void setMessage(String msg)
  {
    topLabel.setText(message = msg);
    topLabel.revalidate();
    topLabel.repaint();
  }

  public Object showDialog()
  {
    Dimension frmSize;
    Point loc;
    Dimension dlgSize = getPreferredSize();
    if (parent != null)
    {
      frmSize = parent.getSize();
      loc = parent.getLocation();
    }
    else
    {
      frmSize = Toolkit.getDefaultToolkit().getScreenSize();
      loc = new Point();
    }
    setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                (frmSize.height - dlgSize.height) / 2 + loc.y);

    //  Start our database thread
    dbWorker.start();

    //  Popup the dialog
    show();

    //  Get & return the object result from dbWorker
    // Changed to return object as a more generic WaitDialog

    //    retVec = ( Vector )dbWorker.get();
    //    if( retVec == null )
    //      retVec = new Vector();

    retObj = dbWorker.get();
    return retObj;
  }

  protected void processWindowEvent(WindowEvent e)
  {
    if (e.getID() == WindowEvent.WINDOW_CLOSING)
    {
      if (taskToCancel != null)
      {
	cancel();
	super.processWindowEvent(e);
      }
    }
    else
    {
      super.processWindowEvent(e);
    }
  }

  protected void cancelBtn_actionPerformed(ActionEvent e)
  {
    cancel();
  }

  protected void cancel()
  {
    taskToCancel.cancel();
    dispose();
  }

  public void setNote(String s)
  {
    noteLabel.setText(s);
  }

  public void setLabel(String s)
  {
    topLabel.setText(s);
  }

  public void setDBWorker(SwingWorker dbWorker)
  {
    this.dbWorker = dbWorker;
  }

  public void dispose()
  {
    gifLabel.setIcon(null);
    super.dispose();
  }
}
